import Vue from "vue";
import Vuex from "vuex";
import axios from "axios";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    Authorization: localStorage.getItem('Authorization') ? localStorage.getItem('Authorization') : "",
    usertype: localStorage.getItem('usertype') ? localStorage.getItem('usertype') : "",
    is_active: localStorage.getItem('is_active') ? localStorage.getItem('is_active') : "",
    username: localStorage.getItem('username') ? localStorage.getItem("username") : "",
    unionid: localStorage.getItem('unionid') ? localStorage.getItem("unionid") : "",
    mode:localStorage.getItem('mode') ? localStorage.getItem("mode") : "",
    userid:localStorage.getItem('userid') ? localStorage.getItem("userid") : "",
  },
  mutations: {
    loginChange(state, user) {
      state.username = user.username;
      state.token = user.token;
      state.usertype = user.usertype;
      state.is_active = user.is_active;
      state.unionid = user.unionid;
      state.mode=user.mode;
      state.userid=user.userid;
      localStorage.setItem('username', user.username);
      localStorage.setItem('usertype', user.usertype);
      localStorage.setItem('is_active', user.is_active);
      localStorage.setItem('Authorization', user.token);
      localStorage.setItem('unionid', user.unionid);
      localStorage.setItem('mode', user.mode);
      localStorage.setItem('userid', user.userid);
    },
    loginOut(state) {
      state.Authorization = "";
      state.is_active = "";
      state.usertype = "";
      state.unionid = "";
      state.mode = "";
      state.userid="";
      localStorage.setItem('Authorization', "");
      localStorage.setItem('usertype', "");
      localStorage.setItem('is_active', "");
      localStorage.setItem('unionid', "");
      localStorage.setItem('mode', "");
      localStorage.setItem('userid', "");
      axios.post("/api/adminapi/signout")
        .then(res => {})
        .catch(err => {})
    },
    resetUnionid(state,unionid){
      state.unionid = unionid;
      localStorage.setItem('unionid', unionid);
    }
  },
  actions: {
    login(context, user) {
      context.commit('loginChange', user);
    },
    loginOut(context) {
      context.commit('loginOut');
    },
    resetUnionid(context,unionid){
      context.commit('resetUnionid',unionid);
    }
  }
});
